<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改人员信息</title>
    <script src="/js/jquery.js"></script>
</head>
<body>
<form>
    <table border="1" style="width: 800px;margin: 0 auto">
        <tr>
            <th colspan="2">添加人员</th>
            <input type="hidden" name="pid">
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="pname"></td>
        </tr>
        <tr>
            <td>性别</td>
            <td><input type="text" name="sex"></td>
        </tr>
        <tr>
            <td>年龄</td>
            <td><input type="text" name="age"></td>
        </tr>
        <tr>
            <td>级别</td>
            <td><input type="text" name="rank"></td>
        </tr>
        <tr>
            <td>所属部门</td>
            <td><input type="text" name="department"></td>
        </tr>
        <tr>
            <th colspan="2">
                <input onclick="submitPerson()" type="button" value="提交">
            </th>

        </tr>
    </table>
</form>
</body>
<script>
    const id = location.search.substr(location.search.indexOf("=") + 1)
    $.getJSON("/check", {id: id}, function (data) {
        $("input[name='pid']").val(data.pid)
        $("input[name='pname']").val(data.pname)
        $("input[name='sex']").val(data.sex)
        $("input[name='age']").val(data.age)
        $("input[name='rank']").val(data.rank)
        $("input[name='department']").val(data.department)
    })

    function submitPerson() {
        const val = $("form").serializeArray()
        // alert(val[0].value)
        for (let v of val) {
            if (v.value == "") {
                alert("请将输入框填写完整，输入框不允许为空")
                return
            }
        }
        $.post("/update", val, function (data) {
            alert(data == 1 ? "修改成功" : "修改失败，请重新输入")
            if (data == 1) {
                location.href = "index.html"
            }
        }, "json")
    }
</script>
</html>